package cn.bdqn.controller.course;

import cn.bdqn.pojo.course.CourseTag;
import cn.bdqn.service.course.CourseTagService;
import cn.bdqn.utils.Json;
import cn.bdqn.utils.RedisCache;
import cn.bdqn.utils.RedisConstants;
import cn.bdqn.vo.course.CourseTagVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api")
public class CourseTagController {
    @Autowired
    private CourseTagService courseTagService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 根据标签id查询标签信息
     * @param courseTagId   标签id
     * @return  返回标签对象
     */
    @GetMapping("/selectCourseTagById")
    public Object selectCourseTagById(Integer courseTagId){
        //检查id是否为空
        if(null == courseTagId||courseTagId < 0){
            return new Json(400,"标签id不能为空");
        }
        CourseTag courseTag = courseTagService.selectCourseTagById(courseTagId);
        return Json.isEmpty(courseTag);
    }

    /**
     * 从缓存中查询所有标签信息，若没有重新向数据库获取
     * @return  返回所有标签信息集合
     */
    @GetMapping("/static/selectAllCourseTag")
    public Object selectAllCourseTag(){
        //从缓存获取标签
        List<CourseTagVo> courseTagList = redisCache.getCacheList(RedisConstants.COURSE_TAG_KEY);
        if(null == courseTagList || courseTagList.size() == 0){
            courseTagList = courseTagService.selectAllCourseTag();

            //检查二、三级标签是否为空，若为空则腹肌标签不显示
            for (int i = 0; i < courseTagList.size(); i++) {
                List<CourseTagVo> twos = courseTagList.get(i).getTagList();
                if(null == twos || twos.size() == 0){
                    courseTagList.remove(i);
                }else {
                    for (int o = 0; o < twos.size(); o++) {
                        List<CourseTagVo> threes = twos.get(o).getTagList();
                        if (null == threes || threes.size() == 0) {
                            twos.remove(o);
                        }
                    }
                }
            }
            //存放到redis缓存中
            redisCache.setCacheList(RedisConstants.COURSE_TAG_KEY,courseTagList);
        }

        return Json.isEmpty(courseTagList);
    }

    /**
     * 通过课程标签名称、状态值、标签id、当前页、数据条数获取分页后的标签信息集合
     * @param tagName   标签名称
     * @param tagStatus 标签状态信息
     * @param tagIds    标签id数组
     * @param currentPage   当前页
     * @param pageSize  数据条数
     * @return  返回标签信息集合
     */
    @PreAuthorize("hasAuthority('course:management')")
    @GetMapping("/selectCourseTagByNameAndStatusAndIdAndPage")
    public Object selectCourseTagByNameAndStatusAndIdAndPage(String tagName,Integer tagStatus,Integer[] tagIds,Integer currentPage,Integer pageSize){
        //检查当前页是否为空
        if (null == currentPage || currentPage < 1) {
            currentPage = 1;
        }
        //检查分页大小是否为空
        if (null == pageSize || pageSize < 1) {
            return new Json(400,"分页大小不能为空");
        }

        //检查标签名称是否为空
        if(null == tagName || tagName.trim().length() == 0){
            tagName = null;
        }

        if(null == tagIds || tagIds.length == 0){
            tagIds = null;
        }
        PageHelper.startPage(currentPage,pageSize);
        List<CourseTagVo> courseTags = courseTagService.selectCourseTagByNameAndStatusAndId(tagIds, tagStatus, tagName);
        if(null == courseTags || courseTags.size() == 0){
            return new Json(400,"数据查询失败");
        }else {
            return new Json(200,"查询成功",new PageInfo<>(courseTags));
        }
    }

    /**
     * 查询一、二级标签
     * @return  返回父标签集合
     */
    @GetMapping("/selectParentTag")
    public Object selectParentTag(){
        List<CourseTagVo> courseTagVos = courseTagService.selectParentTag();
        return Json.isEmpty(courseTagVos);
    }

    /**
     * 获取课程标签数量
     * @return  返回标签数量
     */
    @PreAuthorize("hasAuthority('course:management')")
    @GetMapping("/selectCourseTagCount")
    public Object selectCourseTagCount(){
        Long aLong = courseTagService.selectCourseTagCount();
        return Json.isEmpty(aLong);
    }

    /**
     * 查询所哟一级标签
     * @return  返回一级标签集合
     */
    @GetMapping("/selectCourseTageLevel1")
    public Object selectCourseTageLevel1(){
        List<CourseTag> courseTags = courseTagService.selectCourseTageLevel1();
        return Json.isEmpty(courseTags);
    }

    /**
     * 创建课程标签
     * @param courseTag 课程标签对象
     * @return  返回json对象
     */
    @PreAuthorize("hasAuthority('course:management')")
    @PostMapping("/createCourseTag")
    public Object createCourseTag(@RequestBody CourseTag courseTag){
        Json json = null;
        //检查课程标签是否为空
        if (null == courseTag) {
            json =  new Json(400,"课程标签不能为空");
        }else if (null == courseTag.getCourseTagName()) {
            json =  new Json(400,"标签名称不能为空");
        }else if (null == courseTag.getCourseTagParentId()) {
            json =  new Json(400,"父标签不能为空");
        }else if (null == courseTag.getCourseTagStatus()) {
            json =  new Json(400,"标签状态不能为空");
        }
        if (null == json) {
            //调用添加方法
            Integer integer = courseTagService.insertCourseTag(courseTag);
            if (integer < 0){
                json = new Json(400,"创建失败");
            }else {
                //将redis缓存中的集合设置为空
                redisCache.deleteObject(RedisConstants.COURSE_TAG_KEY);
                json = new Json(200,"添加成功");
            }
        }

        return  json;
    }

    /**
     * 更新标签信息
     * @param courseTag 更新后的课程标签对象
     * @return  返回json对象
     */
    @PreAuthorize("hasAuthority('course:management')")
    @PutMapping("/updateCourseTag")
    public Object updateCourseTag(@RequestBody CourseTag courseTag){
        Json json = null;
        if (null == courseTag) {
            json =  new Json(400,"课程标签不能为空");
        }else if(null == courseTag.getCourseTagId()){
            json =  new Json(400,"标签id不能为空");
        }else if (null == courseTag.getCourseTagName()) {
            json =  new Json(400,"标签名称不能为空");
        }else if (null == courseTag.getCourseTagParentId()) {
            json =  new Json(400,"父标签不能为空");
        }else if (null == courseTag.getCourseTagStatus()) {
            json =  new Json(400,"标签状态不能为空");
        }
        if (null == json) {
            Integer integer = courseTagService.updateCourseById(courseTag);
            if(integer<0){
                json = new Json(400,"更新失败");
            }else {
                //将redis缓存中的集合设置为空
                redisCache.deleteObject(RedisConstants.COURSE_TAG_KEY);
                json = new Json(200,"更新成功");
            }
        }
        return json;
    }

    /**
     * 根据课程标签id修改课程标签状态属性
     * @param tagId 课程标签id
     * @param status    修改后的课程标签状态信息
     * @return  返回json对象
     */
    @PreAuthorize("hasAuthority('course:management')")
    @PutMapping("/updateCourseTagStatus")
    public Object updateCourseTagStatus(@RequestParam("tagId") Integer tagId,@RequestParam("status") Integer status){
        //检查id是否为空
        Json json = null;
        if(null == tagId){
            json =  new Json(400,"标签id不能为空");
        }else if (null == status) {
            json =  new Json(400,"标签状态不能为空");
        }

        if (null == json) {
            Integer integer = courseTagService.updateCourseTagStatusById(tagId, status);
            if(integer<0){
                json = new Json(400,"更新失败");
            }else {
                //将redis缓存中的集合设置为空
                redisCache.deleteObject(RedisConstants.COURSE_TAG_KEY);
                json = new Json(200,"更新成功");
            }
        }
        return json;
    }

}
